package cn.picclife.mwx.manager.sys.controller;

import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.picclife.mwx.common.core.util.UUIDUtil;
import cn.picclife.mwx.common.web.base.BaseResult;
import cn.picclife.mwx.manager.sys.model.bo.ApprovalInfo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.PageResponse;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.service.ApprovalService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@RequestMapping("approval")
@RestController
public class ApprovalController {

	private Log log = LogFactory.getLog(ApprovalController.class);
	
    @Autowired
    private ApprovalService approvalService;


    @RequestMapping(value="/apply", method= {RequestMethod.POST})
    public BaseResult applySubmit(HttpServletRequest request, @RequestBody ApprovalInfo approvalInfo){
        log.info("接口开始" + JSONObject.toJSON(approvalInfo).toString());
        BaseResult baseResult = new BaseResult();
        ApprovalInfo ApprovalInfo = new ApprovalInfo();
        //获取用户登录信息
        try {
            UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
            //获取前端传值
            String userOptName = user.getOptName();
            String deptName = user.getDeptName();
            String optNo = user.getOptNo();
            String reqId = UUIDUtil.genUUID();
            approvalInfo.setReqId(reqId);
            approvalInfo.setDept(deptName); // user.getDeptName()
            approvalInfo.setOprator(optNo); //user.getOptNo()
            approvalInfo.setOptName(userOptName);//user.getOptName()
            approvalInfo.setApprovalId("");
            approvalInfo.setApproval("");
            approvalInfo.setApprovalName("");
            //approvalInfo.setReqTime(new Date());
        } catch (Exception e) {
        	log.info(e);
        }
        log.info("申请信息为："+approvalInfo.toString());
        BaseResult result = approvalService.applySubmit(approvalInfo);
        return result;
    }

    @RequestMapping(value="/query", method= {RequestMethod.POST})
    public PageResponse queryApprovalInfo(HttpServletRequest request,@RequestBody ApprovalInfo approvalInfo) {
//        BaseResult baseResult = new BaseResult();
    	PageResponse pageResponse = new PageResponse();
    	PageCount pageCount = new PageCount();
        log.info("kaishi");
        UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
        log.info("user:"+JSONObject.toJSON(user).toString());
        //获取前端传值
        String deptNo = user.getDeptNo(); //当前用户的机构编码
        //String userOptName = user.getOptName();
        String deptName = user.getDeptName();
        String optNo = user.getOptNo();
        approvalInfo.setDept(deptName); // user.getDeptName()
        approvalInfo.setOprator(optNo); //user.getOptNo()
        if("00".equals(approvalInfo.getAppType())) {
        	approvalInfo.setOptName(user.getOptName());
        	approvalInfo.setAppType("0");
        }
        //approvalInfo.setOptName(userOptName);//user.getOptName()
        try {
        	pageCount =  approvalService.queryByReqDetail(approvalInfo,deptNo);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========query接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
        
        /*if(StringUtils.isBlank(approvalInfo.getOptName())){
        	
        	//申请人姓名为空，申请事项也为空
            baseResult.setRet(BaseResult.BIZ_ERROR);
            baseResult.setMsg("输入数据不能为空，请重新输入");
            //申请人姓名为空，申请事项不为空，以申请事项查询
            if(StringUtils.isNotEmpty(approvalInfo.getReqDetail())){
               ApprovalInfo approvalInfo1 =  approvalService.queryByReqDetail(approvalInfo);
               baseResult.setRet(BaseResult.SUCCESS);
               baseResult.setMsg("查询成功");
               baseResult.setData(approvalInfo1);
            }
        }else {//申请人姓名不为空，申请事项为空，以申请人姓名查询
           if(StringUtils.isBlank(approvalInfo.getReqDetail())){
               ApprovalInfo approvalInfo1 = approvalService.queryByOptName(approvalInfo);
               baseResult.setRet(BaseResult.SUCCESS);
               baseResult.setMsg("查询成功");
               baseResult.setData(approvalInfo1);
           }else {//申请人姓名不为空，申请人姓名不为空
               ApprovalInfo approvalInfo1 = approvalService.queryApprovalInfo(approvalInfo);
               baseResult.setRet(BaseResult.SUCCESS);
               baseResult.setMsg("查询成功");
               baseResult.setData(approvalInfo1);
           }

        }*/
        pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
        pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
        pageResponse.setPageCount(pageCount);
		log.info("===========query接口返回报文："+JSONObject.toJSON(pageResponse).toString());
		return pageResponse; 
		}

    @RequestMapping(value="/update", method= {RequestMethod.POST})
    public BaseResult updateApprovalInfo(HttpServletRequest request,@RequestBody ApprovalInfo approvalInfo){
        BaseResult baseResult = new BaseResult();
        approvalInfo.setApprovalId("admin");
        approvalInfo.setApprovalName("管理员");
        try {
        	approvalService.updateApprovalInfo(approvalInfo);
        }catch(Exception e){
            log.error("异常信息"+e.getMessage(),e);
        	baseResult.setRet(BaseResult.BIZ_ERROR);
        	baseResult.setMsg("插入失败");
        }
        baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("插入数据成功");
        return baseResult;
    }
    
    /**
	 * 手动输入申请理由，并保存进对应的申请记录
	 * @param approvalInfo
	 * @return
	 */
	@RequestMapping(value="/updateApplyInfo", method= {RequestMethod.POST})
	public BaseResult updateApply(HttpServletRequest request,@RequestBody ApprovalInfo approvalInfo){
		BaseResult baseResult = new BaseResult();
		 log.info("接口开始" + JSONObject.toJSON(approvalInfo).toString());
		 ApprovalInfo ApprovalInfo = new ApprovalInfo();
	        //获取用户登录信息
	        try {
	            UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
	            String reqId = UUIDUtil.genUUID();
	            approvalInfo.setReqId(reqId);
	            log.info("==========user========"+JSONObject.toJSON(user).toString());
	            //获取前端传值
	            String userOptName = "";
 	            String deptName = "";
 	            String optNo = "";

	            if(user!=null){
	            	userOptName = user.getOptName();
	 	            deptName = user.getDeptName();
	 	            optNo = user.getOptNo();
	            }
	           
	            
	            approvalInfo.setDept(deptName); // user.getDeptName()
	            approvalInfo.setOprator(optNo); //user.getOptNo()
	            approvalInfo.setOptName(userOptName);//user.getOptName()
	            approvalInfo.setApprovalId("");
	            approvalInfo.setApproval("");
	            approvalInfo.setApprovalName("");
	            approvalInfo.setReqTime(new Date());
	            
	        } catch (Exception e) {
	            log.info(e);
	        }
	        log.info("申请信息为："+approvalInfo.toString());
		try {
			approvalService.updateApplyInfo(approvalInfo);
		 }catch(Exception e){
            log.error("异常信息"+e.getMessage(),e);
        	baseResult.setRet(BaseResult.BIZ_ERROR);
        	baseResult.setMsg("申请理由插入失败");
        }
        baseResult.setRet(BaseResult.SUCCESS);
        baseResult.setMsg("插入申请理由成功");
        return baseResult;
	}
}
